package com.itheima.service.impl;

import com.itheima.mapper.MemberMapper;
import com.itheima.service.MemberService;
import com.itheima.vo.MemberReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    /**
     * 获取会员数量统计数据
     * @return
     */
    @Override
    public MemberReportVO getMemberReport() {
        //获取每个月最后一天放进dateList集合
        List<String> dateList = new ArrayList<>();
        List<Integer>memberCountList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            YearMonth yearMonth = YearMonth.now();
            yearMonth = yearMonth.minusMonths(12);
            yearMonth=yearMonth.plusMonths(i);
            LocalDate yearDate = yearMonth.atEndOfMonth();
            Integer memberCountByYearDate = memberMapper.findMemberCountByYearDate(yearDate);
            memberCountList.add(memberCountByYearDate);
            String formatLocalDate = yearDate.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
            dateList.add(formatLocalDate);
        }
        MemberReportVO memberReportVO = MemberReportVO.builder()
                .months(dateList)
                .memberCount(memberCountList)
                .build();
        return memberReportVO;
    }
}
